package com.smartwasser.yunhtgl.apphtgl.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dou361.dialogui.DialogUIUtils;
import com.dou361.dialogui.listener.DialogUIItemListener;
import com.smartwasser.yunhtgl.apphtgl.R;
import com.smartwasser.yunhtgl.apphtgl.base.BaseActivity;
import com.smartwasser.yunhtgl.apphtgl.base.BaseConstant;
import com.smartwasser.yunhtgl.apphtgl.utils.Logger;
import com.smartwasser.yunhtgl.apphtgl.utils.PhotoUtils;
import com.smartwasser.yunhtgl.apphtgl.widget.CustomToast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.graphics.BitmapFactory.decodeFile;
import static com.smartwasser.yunhtgl.apphtgl.utils.PhotoUtils.compressImage;
import static com.smartwasser.yunhtgl.apphtgl.utils.PhotoUtils.getOptions;
import static com.smartwasser.yunhtgl.apphtgl.utils.PhotoUtils.pickImageFromAlbum;
import static com.smartwasser.yunhtgl.apphtgl.utils.PhotoUtils.startPhotoZoom;

public class SealLoadActivity extends BaseActivity {
    private Handler handler = new Handler();
    private Dialog loadingHorizontal;
    private Uri imageUri;
    @BindView(R.id.tv_toolbar)
    TextView tv_toolbar;
    @BindView(R.id.image1)
    ImageView image1;

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_seal_load);
        ButterKnife.bind(this);
    }

    @Override
    public void initView() {
        tv_toolbar.setText("用印备案上传");
    }

    @Override
    public void initData() {
    }

    @OnClick({R.id.back, R.id.image1, R.id.seal_load, R.id.seal_cancel})
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.image1:
                takePhoto();
                break;
            case R.id.seal_load:
                loadingHorizontal = DialogUIUtils.showLoadingHorizontal(this, "上传中...").show();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        DialogUIUtils.dismiss(loadingHorizontal);
                        CustomToast.showToast(getApplication(), "上传成功");
                        finish();
                    }
                }, 2000);
                break;
            case R.id.seal_cancel:
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Logger.e("requestCode = " + requestCode);
        Logger.e("******");
        Logger.e("resultCode = " + resultCode);
        if (resultCode != RESULT_CANCELED) {
            try {
                switch (requestCode) {
                    case BaseConstant.TAKE_PHOTO://拍照
                        imageUri = startPhotoZoom(this, imageUri, BaseConstant.CUTS_PHOTO);
                        break;
                    case BaseConstant.PICK_PHOTO://相册
                        Uri uri = data.getData();
                        imageUri = startPhotoZoom(this, uri, BaseConstant.CUTS_PHOTO);
                        break;
                    case BaseConstant.CUTS_PHOTO://图片裁剪质量压缩
                        if (data != null) {
                            Bitmap bitmap = compressImage(decodeFile(imageUri.getPath(), getOptions(imageUri.getPath())));
                            image1.setImageBitmap(bitmap);
                        }
                        break;
                }
            } catch (IOException e) {
                CustomToast.showToast(this, "出现问题,此时无法拍照");
            }
        }
    }

    public void takePhoto() {
        List<String> strings = new ArrayList<>();
        strings.add("拍照");
        strings.add("图库");
        DialogUIUtils.showBottomSheetAndCancel(this, strings, "取消", new DialogUIItemListener() {
            @Override
            public void onItemClick(CharSequence text, int position) {
                switch (position) {
                    case 0:
                        try {
                            imageUri = PhotoUtils.takePhoto(SealLoadActivity.this, BaseConstant.TAKE_PHOTO);
                        } catch (IOException e) {
                            CustomToast.showToast(SealLoadActivity.this, "出现问题,此时无法拍照");
                        }
                        break;
                    case 1:
                        pickImageFromAlbum(SealLoadActivity.this, BaseConstant.PICK_PHOTO);
                        break;
                }
            }

            @Override
            public void onBottomBtnClick() {
                DialogUIUtils.dismiss();
            }
        }).show();
    }
}
